/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package PatientAdministrationBeans;

import EntityClass.Doctor;
import EntityClass.Schedule;
import Exception.ExistException;
import java.sql.Time;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Ashish
 */
@Stateless
public class SchedulingandResourceAllocationBean implements Interface.SchedulingandResourceAllocationBeanRemote {

@PersistenceContext()
EntityManager em;
Schedule schedule;
Doctor doctor;
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void addDoctor(String id, String name, String dob, String username) throws ExistException{

        doctor = em.find(Doctor.class, id);
        if (doctor != null)
            throw new ExistException("DOCTOR ALREADY EXISTS.");
        doctor =  new Doctor();
        doctor.create(id, name, username, dob);
        em.persist(doctor);
        System.out.println("Doctor " + id + " successfully added.");
    }
    
    public String getDoctorName(String id) throws ExistException{
        doctor = em.find(Doctor.class, id);
        if (doctor == null)
            throw new ExistException("DOCTOR ID DOES NOT EXIST.");
        
        return doctor.getName();
    }
    
    public String getAvailableDoctors(String appointmentDate, String appointmentTime) {
     
     String pattern = "HH:mm";
     String shiftCode=null;
     
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            try {
                Date sA = sdf.parse("00:00");
                Date eA= sdf.parse("07:59");
                Date sB= sdf.parse("08:00");
                Date eB= sdf.parse("15:59");
                Date sC = sdf.parse("16:00");
                Date eC= sdf.parse("23:50");
                
                Date appTime = sdf.parse(appointmentTime);

                
                if((appTime.compareTo(eA)== 1) && (appTime.compareTo(sA)== -1)){
                    shiftCode="A";
                }
                else if((appTime.compareTo(eB)== 1) && (appTime.compareTo(sB)== -1)){
                    shiftCode="B";
                }
                else if((appTime.compareTo(eC)== 1) && (appTime.compareTo(sC)== -1)){
                    shiftCode="C";
                }
                                
            } catch (ParseException e){
                // Exception handling goes here
            }
        
                  
     Query q = em.createQuery
                ("SELECT s FROM Schedule s WHERE s.shiftDate = :appointmentDate AND s.shiftCode=:shiftCode");
        q.setParameter("appointmentDate", appointmentDate);
        q.setParameter("shiftCodeTime", shiftCode);
        List result;
        result = q.getResultList();
        if (!(result.isEmpty())){
        em.clear();
 //       throw new ExistException("CANNOT DELETE A MERCHANT THAT HAS A REWARD TYPE ASSOCIATED WITH IT");
        }
        for (Object o: result){
            schedule = (Schedule)o;
        }
        
        doctor = schedule.getDoctor();
        
        return doctor.getName();
    }
 
    
   
    public List<Doctor> getDoctors() throws ExistException {
        
        Query q = em.createQuery("SELECT c FROM doctor c");
        List docList = new ArrayList();
        for (Object o: q.getResultList()) {
            Doctor doc = (Doctor)o;
            docList.add(doc);
        }
        if (docList.isEmpty()==true){
            throw new ExistException("THERE ARE NO DOCTORS IN THE DATABASE");
        }

      
        
        return docList;
    }
    
    public List<Schedule> getShifts(String doctorID) throws ExistException{
        Query q = em.createQuery("SELECT c FROM Schedule c INNER JOIN c.doctor p WHERE p.id=:doctorID");
        q.setParameter("doctorID", doctorID);
        List scheduleList = new ArrayList();
        for (Object o: q.getResultList()) {
            Schedule sch = (Schedule)o;
            scheduleList.add(sch);
        }
        if (scheduleList.isEmpty()==true){
            throw new ExistException("THERE ARE NO SHIFTS ASSIGNED TO THIS DOCTOR");
        }
            
        return scheduleList;
        
    }
 
 

    public void assignShift (String doctorId, String shiftDate, String shiftCode) throws ExistException{
     
     Query q = em.createQuery
                ("SELECT s FROM Schedule s WHERE s.shiftDate = :shiftDate AND s.shiftCode =:shiftCode");
        q.setParameter("shiftDate", shiftDate);
        q.setParameter("shiftCode", shiftCode);
        List result;
        result = q.getResultList();
        if ((result.isEmpty())){
        em.clear();
        throw new ExistException("SHIFT DOES NOT EXIST");
        }
        for (Object o: result){
            schedule = (Schedule)o;
        }
        
        doctor = em.find(Doctor.class, doctorId);
        doctor.addSchedule(schedule);
        schedule.setDoctor(doctor);
        em.persist(schedule);
        em.persist(doctor);
        
       
        
 }
 
    public void createShift(String shiftDate, String shiftCode) throws ExistException{
   Query q = em.createQuery("SELECT c FROM Schedule c where c.shiftDate=:shiftDate AND c.shiftCode=:shiftCode");
        q.setParameter("shiftDate", shiftDate);
        q.setParameter("shiftCode", shiftCode);
        List result;
        result = q.getResultList();
        if (!(result.isEmpty()))
            throw new ExistException("SHIFT ALREADY CREATED.");

        schedule = new Schedule();
        schedule.create(shiftDate, shiftCode);
        em.persist(schedule);
 }
    
    public List<Schedule> viewShifts() throws ExistException{
                Query q = em.createQuery("SELECT c FROM Schedule c");
        List scheduleList = new ArrayList();
        for (Object o: q.getResultList()) {
            Schedule sch = (Schedule)o;
            scheduleList.add(sch);
        }
        if (scheduleList.isEmpty()==true){
            throw new ExistException("THERE ARE NO SCHEDULES IN THE DATABASE");
        }
   
        
        return scheduleList;
    }
 
    public void DisplayPatientInfo() throws Exception {  
         
     }   


}
